<?php

namespace Frieza\Modules\Oauthadmin\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\Member;
use Doraemon\service\Member as sMember;
use Doraemon\service\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;


class BanerListController extends Controller
{
    public function index(Request $request, Content $content)
    {

        $page = $request->input("page", 1);
        $pageSize = $request->input("per_page", 20);
        $query = $request->all();

        if (!empty($query['nick'])){
            $data['nick[~]'] = $query['nick'];
        }
        if (!empty($query['uid'])){
            $data['uid'] = $query['uid'];
        }

        $data['status'] = Member::STATUS_FROZEN;
        $counts = Member::countUserNum($data);

        $data['ORDER'] = ['edittime' => 'DESC'];
        $data['LIMIT'] = [($page - 1) * $pageSize, $pageSize];

        $infos = Member::getUserInfoByWhere( "*",$data);

        $gridModel = new pGridDataModel($infos, $counts, ['perPage' => $pageSize]);

        $listGrid = Admin::grid($gridModel, function(Grid $grid){
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableColumnSelector();
            $grid->expandFilter();

            $grid->filter(function ($filter){

                $filter->disableIdFilter();

                $filter->column(8, function ($filter){
                    $filter->dropDown('Find', [
                        'nick' => '昵称',
                        'uid' => '用户ID'
                    ]);
                });

                $filter->expend();
            });

            $grid->column('avatar', '头像')->display(function() {
                $userInfo = sMember::getUserInfoByUid($this->uid);
                $avatarPicUrl = User::getUserAvatarPicUrl($this->uid, $userInfo["avatar_version"]);
                return '<img src = "' .$avatarPicUrl[0].'"width = 45px heigh = 45px/>';
            });
            $grid->column('nick', '昵称')->display(function(){
                return "<a href='/admin/user/Detail/$this->uid' target='_blank' >" . $this->nick . "</a>";
            })->width(200);
            $grid->column('uid', '用户ID');
            $grid->column('addtime', '注册时间')->display(function(){
                return date("Y-m-d H:i:s", $this->addtime);
            });
            $grid->column('edittime', '封禁时间')->display(function(){
                return date("Y-m-d H:i:s", $this->edittime);
            });
            $grid->column('identity_mark', '标签');

        });

        $tab = new Tab();
        $tab->add('封禁用户', $listGrid->render());
        $content->row($tab);
        return $content->header('封禁用户列表')->breadcrumb(['text'=>'封禁用户列表']);

    }
}